package com.yegs.jltqc.U8ServiceImpl;

import com.yegs.jltqc.TO.OutComePageQuery;
import com.yegs.jltqc.U8Service.U8OutComeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class U8OutComeServiceImpl implements U8OutComeService {

    @Autowired
    @Qualifier("u8JdbcTemplate")
    private JdbcTemplate u8JdbcTemplate;

    @Override
    public Page<Map<String, Object>> query(OutComePageQuery query) {
        String sql = "SELECT TOP " + (query.getPageIndex()) * query.getPageSize()+
                " ROW_NUMBER() OVER(ORDER BY autoid ASC) AS ROWID,* from qc_outcome where 1=1 ";
        if(query.getCinvname() != null && query.getCinvname().trim().length() > 0){
            sql += " and cInvName like '%"+query.getCinvname().trim()+"%'";
        }
        if(query.getSddate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate >= '"+sdf.format(query.getSddate())+"'";
        }
        if(query.getDdate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate = '"+sdf.format(query.getDdate())+"'";
        }
        if(query.getEddate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate <= '"+sdf.format(query.getEddate())+"'";
        }
        if(query.getCvenname() != null && query.getCvenname().trim().length() > 0){
            sql += " and ccusname like '%"+query.getCvenname().trim()+"%'";
        }
        if(query.getCdlcode()!= null && query.getCdlcode().trim().length() > 0){
            sql += " and cdlcode like '%"+query.getCdlcode().trim()+"%'";
        }
        sql = "select * from ("+sql +") as temp1 where ROWID > " + (query.getPageIndex()-1) * query.getPageSize();
        List<Map<String, Object>> list = u8JdbcTemplate.queryForList(sql);
        sql = "select count (*) from qc_outcome where 1=1 ";
        if(query.getCinvname() != null && query.getCinvname().trim().length() > 0){
            sql += " and cInvName like '%"+query.getCinvname().trim()+"%'";
        }
        if(query.getSddate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate >= '"+sdf.format(query.getSddate())+"'";
        }
        if(query.getDdate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate = '"+sdf.format(query.getDdate())+"'";
        }
        if(query.getEddate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate <= '"+sdf.format(query.getEddate())+"'";
        }
        if(query.getEddate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sql += " and ddate <= '"+sdf.format(query.getEddate())+"'";
        }
        if(query.getCvenname() != null && query.getCvenname().trim().length() > 0){
            sql += " and cvenname like '%"+query.getCvenname().trim()+"%'";
        }
        if(query.getCdlcode() != null && query.getCdlcode().trim().length() > 0){
            sql += " and cdlcode like '%"+query.getCdlcode().trim()+"%'";
        }
        int total = u8JdbcTemplate.queryForObject(sql,Integer.class);
        PageRequest request = PageRequest.of(query.getPageIndex() -1,query.getPageSize());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        list.forEach(map -> { map.put("dDate",sdf.format((Date) map.get("dDate"))); });
        PageImpl page = new PageImpl(list,request,total);
        return page;
    }

    @Override
    public Map<String, Object> get(Long id) {
        String sql = "select * from qc_produce where AutoId = ? ";
        Map<String, Object> map = u8JdbcTemplate.queryForMap(sql, id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        map.put("dDate",sdf.format((Date) map.get("dDate")));
        return map;
    }

    @Override
    public void finishByAutoid(Long autoid) {

    }
}
